package com.lou.springboot.controller.admin;

import com.lou.springboot.common.ResultVO;
import com.lou.springboot.common.ResultVOUtil;
import com.lou.springboot.entity.AdminUser1;
import com.lou.springboot.enums.ResultEnum;
import com.lou.springboot.service.AdminUser1Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Guaguaerhao on 2020/7/9 23:05
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminUser1Service adminUser1Service;

    @GetMapping("login")
    public String login(HttpServletRequest request){
        return "admin/login";
    }

    @PostMapping("login")
    public String login(@RequestParam("userName") String userName,
                          @RequestParam("password") String password,
                          @RequestParam("verifyCode") String verifyCode,
                          HttpSession session){
        System.out.println(userName + " " + password + " " + verifyCode);
        if (StringUtils.isEmpty(verifyCode)) {
            session.setAttribute("errorMsg", "验证码不能为空");
            return "admin/login";
        }
        if (StringUtils.isEmpty(userName) || StringUtils.isEmpty(password)) {
            session.setAttribute("errorMsg", "用户名或密码不能为空");
            return "admin/login";
        }
        String kaptchaCode = session.getAttribute("verifyCode") + "";
        if (StringUtils.isEmpty(kaptchaCode) || !verifyCode.equals(kaptchaCode)) {
            session.setAttribute("errorMsg", "验证码错误");
            return "admin/login";
        }
        AdminUser1 loginUser = adminUser1Service.login(userName, password);
        if (loginUser!=null){
            session.setAttribute("loginUser", loginUser.getNickName());
            session.setAttribute("loginUserId", loginUser.getAdminUserId());
            // 设置过期时间为2小时 => 2 * 60 * 60 = 7200
            session.setMaxInactiveInterval(7200);
            return "redirect:/admin/index";
        } else {
            session.setAttribute("errorMsg", "用户名或密码错误");
            return "admin/login";
        }
    }

    @GetMapping("logout")
    public String logout(HttpSession session){
        session.setAttribute("loginUser", "");
        session.setAttribute("loginUserId", "");
        session.setMaxInactiveInterval(-1);
        return "redirect:/admin/login";
    }

    @GetMapping({"","/","/index","/index.html"})
    public String index(HttpServletRequest request){
        request.setAttribute("path", "index");
        return "admin/index";
    }

    // 个人档案
    @GetMapping("/profile")
    public String profile(HttpServletRequest request){
        Integer loginUserId = (int)request.getSession().getAttribute("loginUserId");
        AdminUser1 adminUser = adminUser1Service.getUserDetailById(loginUserId);
        if (adminUser == null) {
            return "admin/login";
        }
        request.setAttribute("path","profile");
        request.setAttribute("loginUserName",adminUser.getLoginUserName());
        request.setAttribute("nickName", adminUser.getNickName());
        return "admin/profile";
    }

    // 修改密码
    @PutMapping("/profile/password")
    @ResponseBody
    public ResultVO passwordUpdate(HttpServletRequest request,
                             @RequestParam("originalPassword") String originalPassword,
                             @RequestParam("newPassword") String newPassword){
        if (StringUtils.isEmpty(originalPassword) || StringUtils.isEmpty(newPassword)) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        Integer loginUserId = (int)request.getSession().getAttribute("loginUserId");
        if (adminUser1Service.updatePassword(loginUserId, originalPassword, newPassword)) {
            request.getSession().removeAttribute("loginUserId");
            request.getSession().removeAttribute("loginUser");
            request.getSession().removeAttribute("errorMsg");
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(ResultEnum.ORIGINAL_PASSWORD);
        }
    }

    @PutMapping("/profile/name")
    @ResponseBody
    public ResultVO nameUpdate(HttpServletRequest request,
                               @RequestParam("loginUserName") String loginUserName,
                               @RequestParam("nickName") String nickName){
        if (StringUtils.isEmpty(loginUserName) || StringUtils.isEmpty(nickName)) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        Integer loginUserId = (int)request.getSession().getAttribute("loginUserId");
        if (adminUser1Service.updateName(loginUserId, loginUserName, nickName)) {
            request.getSession().setAttribute("loginUser", nickName);
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error(ResultEnum.ORIGINAL_PASSWORD);
        }
    }
}
